package ooday04;

public class AnimalTest {
    public static void main(String[] args) {
/*
        //演示向上造型（多态）的第二点应用
        Master master =new Master();
        Dog dog = new Dog("小黑",2,"黑色");
        Fish fish = new Fish("小金",2,"金色");
        Chick chick = new Chick("小白",2,"白色");
        master.feed(dog);//再传参的同时，系统自己做了向上造型
        master.feed(fish);
        master.feed(chick);

        Animal o  =new Dog("小黑",2,"黑色");
        Dog d= (Dog) o;//引用o的
        Swim s = (Swim) o ;
        Fish f =(Fish)o;

        System.out.println(o instanceof Dog);//true
        System.out.println(o instanceof Swim);//true
        System.out.println(o instanceof Fish);//false
*/
    Animal[] animals= new Animal[5];
    animals[0] = new Dog("小白",2,"白色");
    animals[1] = new Dog("小黑",2,"黑色");
    animals[2] = new Dog("小红",2,"红色");
    animals[3] = new Dog("小黄",2,"黄色");
    animals[4] = new Dog("小绿",2,"绿色");

        for (int i = 0; i < animals.length; i++) {
            System.out.println(animals[i].name);
            System.out.println(animals[i].age);
            System.out.println(animals[i].color);
            animals[i].eat();
            animals[i].drink();
            if (animals[i] instanceof Dog){
                Dog dog =(Dog) animals[i];
                dog.LookHome();
            }
            if (animals[i] instanceof Chick){
                Chick chick = (Chick) animals[i];
                chick.layEggs();
            }
            if (animals[i] instanceof Fish){
                Fish fish= (Fish) animals[i];
                fish.swim();
            }
        }





       /* //演示向上造型（多态）的第一点应用
//        Animal a = new Animal();//抽象类不能被实例化
        Animal[] animal = new Animal[5];//创建数组对象
        animal[0] = new Dog("小白",2,"白色");//向上造型
        animal[1] = new Chick("小黑",2,"黑色");
        animal[2] = new Chick("小黑",2,"黑色");
        animal[3] = new Fish("小红",2,"红色");
        animal[4] = new Fish("小红",2,"红色");
        for (int i = 0; i < animal.length; i++) {//遍历所有动物
            System.out.println(animal[i].name);
            animal[i].eat();//每个动物吃饭
            animal[i].drink();//每个动物喝水
        }*/



        /*Dog dog= new Dog("小黄",12,"黑色");
        System.out.println(dog.name);*/
/*        dog=null;//表示空，没有指向任何对象
        System.out.println(dog.name);*/
        /*Dog[] dog = new Dog[3];
        dog[0]=new Dog("小绿",5,"绿色");
        dog[1]=new Dog("小黄",5,"黄色");
        dog[2]=new Dog("小白",5,"白色");

        for (int i =0;i< dog.length;i++){
            System.out.println(dog[i].name);
            dog[i].eat();
        }
        *//*Dog x= new Dog("小明",5,"绿色");
        x.sayHi();
        x.eat();
        x.drink();*//*
        Chick[] chicks=  new Chick[5];
        chicks[0]=new Chick("小绿",5,"绿色");
        chicks[1]=new Chick("小绿",5,"绿色");
        chicks[2]=new Chick("小绿",5,"绿色");
        chicks[3]=new Chick("小绿",5,"绿色");
        chicks[4]=new Chick("小绿",5,"绿色");
        for (int i = 0; i < chicks.length; i++) {
            System.out.println(chicks[i].name);
            chicks[i].drink();
        }
       *//* Chick y= new Chick("轩",5,"白");
        y.sayHi();
        y.eat();
        y.drink();*//*

        Fish[] fish = new Fish[6];
        fish[0]= new Fish("小绿",5,"绿色");
        fish[1]= new Fish("小绿",5,"绿色");
        fish[2]= new Fish("小绿",5,"绿色");
        fish[3]= new Fish("小绿",5,"绿色");
        fish[4]= new Fish("小绿",5,"绿色");
        fish[5]= new Fish("小绿",5,"绿色");
        for (int i = 0; i < fish.length; i++) {
            System.out.println(fish[i].name);
            fish[i].sayHi();
        }


        *//*Fish e= new Fish("轩",5,"白");
        e.sayHi();
        e.eat();
        e.drink();*/
    }
}
